package com.luculent.base;

import java.net.InetSocketAddress;
import java.util.Queue;

import org.littleshoot.proxy.ChainedProxy;
import org.littleshoot.proxy.ChainedProxyAdapter;
import org.littleshoot.proxy.ChainedProxyManager;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import net.lightbody.bmp.util.BrowserMobHttpUtil;

public class FChainedProxyManager implements ChainedProxyManager{ 
	
	private InetSocketAddress chainedProxy;
	private String chainedProxyCredentials;

	
	@Override
    public void lookupChainedProxies(HttpRequest httpRequest, Queue<ChainedProxy> chainedProxies) {
        final InetSocketAddress upstreamProxy = chainedProxy;
        if (upstreamProxy != null) {
        	 
            chainedProxies.add(new ChainedProxyAdapter() {
                @Override
                public InetSocketAddress getChainedProxyAddress() {
                	System.out.println(upstreamProxy.getHostName());
                    return upstreamProxy;
                }

                @Override
                public void filterRequest(HttpObject httpObject) {
                    String chainedProxyAuth = BrowserMobHttpUtil.base64EncodeBasicCredentials("", "");
                    if (chainedProxyAuth != null) {
                        if (httpObject instanceof HttpRequest) {
                            HttpHeaders.addHeader((HttpRequest)httpObject, HttpHeaders.Names.PROXY_AUTHORIZATION, "Basic " + chainedProxyAuth);
                        }
                    }
                }
            });
            System.out.println(chainedProxies);
        }
    }
	
	public void setChainedProxy(InetSocketAddress chainedProxy) {
	    //this.prevChainedProxy = this.chainedProxy;
	    this.chainedProxy = chainedProxy;
	}

	public void setChainedProxyCredentials(String chainedProxyCredentials) {
	    this.chainedProxyCredentials = chainedProxyCredentials;
	}
	
}
